package com.borya.dao.impl;

import com.borya.dao.SdkUserDAO;
import com.borya.framework.annotation.Component;
import com.borya.framework.core.ServerSqlSession;
import com.borya.model.entity.ActivationPreAuditLogModel;
import com.borya.model.entity.SdkAppConfModel;
import com.borya.model.entity.SdkUserMapModel;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

@Component("sdkUserDAO")
public class SdkUserDAOImpl extends BaseDAOImpl implements SdkUserDAO {

    private Logger log = Logger.getLogger(getClass());

    @Override
    public ActivationPreAuditLogModel getPreAuditLog(String idCard, String operationType) {
        StringBuilder sql = new StringBuilder(256);
        sql.append("SELECT * FROM tb_activation_pre_audit_log where identityCard = ? and operation_type = ? order by create_time desc limit 1 ");
        try (ServerSqlSession session = getSqlDataSource().session()) {
            return session.executeQuery(sql.toString(), rs -> {
                if (rs != null && rs.next()) {
                    ActivationPreAuditLogModel model = new ActivationPreAuditLogModel();
                    model.setIdentityCard(rs.getString("identityCard"));
                    model.setIdentityCardName(rs.getString("identityCardName"));
                    model.setIdentityCardAddress(rs.getString("identityCardAddress"));
                    model.setPeriod(rs.getString("period"));
                    model.setIsp(rs.getString("isp"));
                    model.setSource(rs.getString("source"));
                    model.setResult(rs.getString("result"));
                    model.setDesc(rs.getString("desc"));
                    model.setCreateTime(rs.getLong("create_time"));
                    return model;
                }
                log.warn("DB not found ActivationPreAuditLogModel,idCard=" + idCard + ",operationType=" + operationType);
                return null;
            }, idCard, operationType);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public SdkUserMapModel getSdkUserMap(String sdkUserId) {
        StringBuilder sql = new StringBuilder(256);
        sql.append("SELECT * FROM tb_sdk_user_map where sdk_user_id = ? ");
        try (ServerSqlSession session = getSqlDataSource().session()) {
            return session.executeQuery(sql.toString(), rs -> {
                if (rs != null && rs.next()) {
                    SdkUserMapModel model = new SdkUserMapModel();
                    model.setDetailType(rs.getInt("detail_type"));
                    model.setSdkUserId(rs.getString("sdk_user_id"));
                    model.setUserId(rs.getString("user_id"));
                    model.setCreateTime(rs.getLong("create_time"));
                    return model;
                }
                log.warn("DB not found getSdkUserMap,sdk_user_id=" + sdkUserId);
                return null;
            }, sdkUserId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public SdkAppConfModel getSdkAppConf(String appId) {
        StringBuilder sql = new StringBuilder(256);
        sql.append("SELECT * FROM tb_sdk_app_conf where app_id = ? ");
        try (ServerSqlSession session = getSqlDataSource().session()) {
            return session.executeQuery(sql.toString(), rs -> {
                if (rs != null && rs.next()) {
                    SdkAppConfModel model = new SdkAppConfModel();
                    model.setDetailType(rs.getInt("detail_type"));
                    model.setSourceType(rs.getInt("source_type"));
                    model.setAppKey(rs.getString("app_key"));
                    model.setLivingCheckFlag(rs.getInt("living_check_flag"));
                    return model;
                }
                log.warn("DB not found getSdkAppConf,sdk_user_id=" + appId);
                return null;
            }, appId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public boolean addSdkAppConf(SdkUserMapModel model) {
        List<String> sql_list = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        sb.append("insert into tb_sdk_user_map set ");
        sb.append("detail_type=").append(model.getDetailType());
        sb.append(",sdk_user_id='").append(model.getSdkUserId()).append("'");
        sb.append(",user_id='").append(model.getUserId()).append("'");
        sb.append(",create_time=").append(model.getCreateTime());
        sql_list.add(sb.toString());

        int result[] = executeBatch(sql_list);
        return result != null && result.length > 0;
    }

    @Override
    public SdkAppConfModel getSdkAppInfoByDetailType(String orderId) {
        StringBuilder sql = new StringBuilder(256);
        sql.append("SELECT detail_type from tb_job_number_audit ");
        sql.append("where order_id = ? ");
        try (ServerSqlSession session = getSqlDataSource().session()) {
            return session.executeQuery(sql.toString(), rs -> {
                if (rs != null && rs.next()) {
                    SdkAppConfModel model = new SdkAppConfModel();
                    model.setDetailType(rs.getInt("detail_type"));
                    return model;
                }
                return null;
            }, orderId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    @Override
    public String getLivingStatusByDetailType(Integer detailType) {
        StringBuilder sql = new StringBuilder(256);
        sql.append("SELECT living_check_flag from tb_sdk_app_conf ");
        sql.append("where detail_type = ? ");
        try (ServerSqlSession session = getSqlDataSource().session()) {
            return session.executeQuery(sql.toString(), rs -> {
                if (rs != null && rs.next()) {
                    return rs.getString("living_check_flag");
                }
                return null;
            }, detailType);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

}